package com.zat.mes.andrmes.utils;

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.net.URI;

/**
 * @author zy
 * @date :2020-05-26 13:51
 * @description:
 */
public class HttpsKitUtils {

    public static String replaceHttpsUrl(String oldUrl) {
        if (TextUtils.isEmpty(oldUrl)) {
            return "";
        }
        String newUrl = oldUrl;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            try {
                URI uri = URI.create(oldUrl);
                String scheme = uri.getScheme();
                String host = uri.getHost();
                String port = String.valueOf(uri.getPort());
                Log.e("zhangsan", "scheme = " + scheme + " host = " + host + " port=" + port);
                if ("https".equals(scheme) && !TextUtils.isEmpty(port)) {
                    String http = oldUrl.replace("https", "http");
                    int portFront = http.indexOf(port) - 1;
                    String substring = http.substring(portFront, portFront + port.length() + 1);
                    newUrl = http.replace(substring, "");
                    return newUrl;
                }
            } catch (Exception e) {

            }

        }
        return newUrl;

    }
}
